package io.zim.modules.ws.handler.impl;

import com.alibaba.fastjson.JSONObject;
import io.zim.common.utils.R;
import io.zim.modules.ws.config.ShowcaseServerConfig;
import io.zim.modules.ws.entity.Const;
import io.zim.modules.ws.entity.IdentityEntity;
import io.zim.modules.ws.entity.ServiceMsg;
import io.zim.modules.ws.handler.WsMsgHandler;
import io.zim.modules.ws.utils.TransitionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.http.common.HttpRequest;
import org.tio.websocket.common.WsRequest;
import org.tio.websocket.common.WsResponse;
import org.tio.websocket.common.WsSessionContext;

import java.util.Map;

/**
 * 客服消息处理器
 * @author
 * @since
 */
public class ServiceMsgHandler implements WsMsgHandler {

  private static Logger log = LoggerFactory.getLogger(ServiceMsgHandler.class);

  private WsRequest wsRequest = null;
  private ServiceMsg serviceMsg  = null;
  private ChannelContext channelContext = null;

  public ServiceMsgHandler() {
  }

  public ServiceMsgHandler(WsRequest wsRequest, ServiceMsg serviceMsg, ChannelContext channelContext) {
    this.wsRequest = wsRequest;
    this.serviceMsg = serviceMsg;
    this.channelContext = channelContext;
  }

  @Override
  public WsResponse handle() {
    try {
      WsSessionContext wsSessionContext = (WsSessionContext) channelContext.getAttribute();
      HttpRequest httpRequest = wsSessionContext.getHandshakeRequest();//获取websocket握手包
      Map params = httpRequest.getParams();
      IdentityEntity identityEntity = (IdentityEntity) TransitionUtils.mapToObject(params,IdentityEntity.class);
      WsResponse wsResponse = null;
      String identity = serviceMsg.getIdentity();
      String sfconnect = serviceMsg.getSfconnect();
      if (sfconnect.equalsIgnoreCase("false")) {//未建立连接
        if (identity.equalsIgnoreCase("service")) {//客服发送的消息
          wsResponse = WsResponse.fromText(JSONObject.toJSONString(R.error(Const.CODE_203,Const.MSG_203).put("data",serviceMsg)), ShowcaseServerConfig.CHARSET);
          Tio.sendToGroup(channelContext.groupContext, identityEntity.getAppKey() + ".service", wsResponse);
          Tio.sendToUser(channelContext.groupContext,identityEntity.getAppKey() + "." + serviceMsg.getTo(),wsResponse);
        } else if (identity.equalsIgnoreCase("user")) {//用户发送的消息
          wsResponse = WsResponse.fromText(JSONObject.toJSONString(R.error(Const.CODE_204,Const.MSG_204).put("data",serviceMsg)), ShowcaseServerConfig.CHARSET);
          Tio.sendToGroup(channelContext.groupContext, identityEntity.getAppKey() + ".service", wsResponse);
        }
      } else {//已建立连接
        wsResponse = WsResponse.fromText(JSONObject.toJSONString(R.error(Const.CODE_202,Const.MSG_202).put("data",serviceMsg)), ShowcaseServerConfig.CHARSET);
        Tio.sendToUser(channelContext.groupContext,identityEntity.getAppKey() + "." + serviceMsg.getTo(),wsResponse);
      }
    } catch (Exception e) {
      return WsResponse.fromText(JSONObject.toJSONString(R.error(Const.CODE_300,e.getMessage())), ShowcaseServerConfig.CHARSET);
    }
    return WsResponse.fromText(JSONObject.toJSONString(R.error(Const.CODE_201,Const.MSG_201)), ShowcaseServerConfig.CHARSET);
  }
}

